﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter21.Code02
{
    using System.IO;

    public class Demo
    {
        public static void Begin()
        {
            {
                FileInfo aFile = new FileInfo(@"C:\Log.txt");
            }
            {
                FileInfo aFile = new FileInfo("Data.txt");
                if (aFile.Exists)
                {
                    Console.WriteLine("File Exists");
                }
            }
            {
                if (File.Exists("Data.txt"))
                {
                    Console.WriteLine("File Exists");
                }
            }
            {
                FileStream aFile = File.OpenRead("Log.txt");
                aFile.Close();

            }
            {
                FileInfo aFileInfo = new FileInfo("Log.txt");
                FileStream aFile = aFileInfo.OpenRead();
                aFile.Seek(8, SeekOrigin.Begin);
                aFile.Seek(2, SeekOrigin.Current);
                aFile.Seek(-5, SeekOrigin.End);
                aFile.Close();
            }
            {
                FileStream aFile = File.OpenRead("Log.txt");
                StreamReader sr = new StreamReader(aFile);
                int charCode;
                charCode = sr.Read();
                while (charCode != -1)
                {
                    Console.Write(Convert.ToChar(charCode));
                    charCode = sr.Read();
                }
                sr.Close();
                Console.ReadLine();
            }
            {
                FileStream aFile = File.OpenRead("Log.txt");
                StreamReader sr = new StreamReader(aFile);
                string line = sr.ReadToEnd();
                Console.WriteLine(line);
                sr.Close();
                Console.ReadLine();
            }
            {
                foreach (string alternativeLine in File.ReadLines("log.txt"))
                {
                    Console.WriteLine(alternativeLine);
                }
                Console.ReadLine();
            }
        }

        public static void BeginEx01()
        {
            byte[] byteData = new byte[200];
            char[] charData = new char[200];
            try
            {
                FileStream aFile = new FileStream("../../Program.cs", FileMode.Open);
                aFile.Seek(144, SeekOrigin.Begin);
                aFile.Read(byteData, 0, 200);
                aFile.Close();
            }
            catch (IOException e)
            {
                Console.WriteLine("An IO exception has been thrown!");
                Console.WriteLine(e.ToString());
                Console.ReadLine();
                return;
            }
            Decoder d = Encoding.UTF8.GetDecoder();
            d.GetChars(byteData, 0, byteData.Length, charData, 0);
            Console.WriteLine(charData);
            Console.ReadLine();
        }

        public static void BeginEx02()
        {
            byte[] byteData;
            char[] charData;
            try
            {
                FileStream aFile = new FileStream("Temp.txt", FileMode.Create);
                charData = "My pink half of the drainpipe.".ToCharArray();
                byteData = new byte[charData.Length];
                Encoder d = Encoding.UTF8.GetEncoder();
                d.GetBytes(charData, 0, charData.Length, byteData, 0, true);
                aFile.Seek(0, SeekOrigin.Begin);
                aFile.Write(byteData, 0, byteData.Length);
                aFile.Close();
            }
            catch (IOException e)
            {
                Console.WriteLine("An IO excepton has been thrown!");
                Console.WriteLine(e.ToString());
                Console.ReadLine();
                return;
            }
            Console.ReadLine();
        }

        public static void BeginEx03()
        {
            string line;
            try
            {
                FileStream aFile = new FileStream("Log.txt", FileMode.Open);
                StreamReader sr = new StreamReader(aFile);
                line = sr.ReadLine();
                while (line != null)
                {
                    Console.WriteLine(line);
                    line = sr.ReadLine();
                }
                sr.Close();
                aFile.Close();
            }
            catch (IOException e)
            {
                Console.WriteLine("An IO exception has been thrown!");
                Console.WriteLine(e.ToString());
                Console.ReadLine();
                return;
            }
            Console.ReadLine();
        }

        public static void BeginEx04()
        {
            try
            {
                FileStream aFile = new FileStream("Log.txt", FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(aFile);
                bool truth = true;
                sw.WriteLine("Hello to you.");
                sw.WriteLine("It is new {0} and thing s are looking good.", DateTime.Now.ToLongDateString());
                sw.Write("More than that,");
                sw.Write(" it's {0} that C# is fun.", truth);
                sw.Close();
                aFile.Close();
            }
            catch (IOException e)
            {
                Console.WriteLine("An IO exception has been thrown!");
                Console.WriteLine(e.ToString());
                Console.ReadLine();
                return;
            }
            Console.ReadLine();
        }
    }
}
